/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */

package com.simon.mengkou.ui.activity;

import android.os.Bundle;
import android.os.Handler;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.User;
import com.simon.mengkou.data.enums.EDirection;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.ui.adapter.CommonFollowAdapter;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : MingXu
 * @date   :  2015年6月3日
 * @desc   : 我的粉丝
 */
public class FanActivity extends BaseTopActivity
		implements PullToRefreshBase.OnRefreshListener2<ListView>{
	@Bind(R.id.common_pulltorefresh_id_list)
	PullToRefreshListView mPtrView;

	private CommonFollowAdapter mAdapter;
	private String mMaxId = CstOuer.PAGE.DEFAULT_ID;
	private String mUserId;
	
	@Override
	protected void init(Bundle savedInstanceState) {
		super.init(savedInstanceState);
		
		mUserId = getIntent().getStringExtra(CstOuer.KEY.USER_ID);

	}
	
	@Override
	protected void initTop() {
		setNavigation(R.drawable.common_ic_left_nav);
		setTitle(R.string.fan_title);
	}

	@Override
	protected void initLayout() {
		setContentView(R.layout.layout_common_pulltorefresh_list);
	}

	@Override
	protected void initViews() {
		ButterKnife.bind(this);

		mPtrView.setOnRefreshListener(this);
		mPtrView.setMode(PullToRefreshBase.Mode.DISABLED);
		mPtrView.setTag(false);

		mAdapter = new CommonFollowAdapter(this);
		mPtrView.setAdapter(mAdapter);

		//首次失败重试
		setOnRetryListener(new OnRetryListener() {
			@Override
			public void onRetry() {
				getData(CstOuer.PAGE.DEFAULT_ID, EDirection.REFRESH);
			}
		});

		//获取数据
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				getData(CstOuer.PAGE.DEFAULT_ID, EDirection.REFRESH);
			}
		}, 500);
	}


	@Override
	public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
		//下拉刷新
		getData(CstOuer.PAGE.DEFAULT_ID, EDirection.REFRESH);
	}

	@Override
	public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
		//上拉加载更多
		getData(mMaxId, EDirection.LOADMORE);
	}

	/**
	 * 获取新粉丝消息
	 * @param maxId
	 * @param direction
	 */
	private void getData(final String maxId, final int direction) {
		AgnettyFuture future = OuerApplication.mOuerFuture.fansList(
				mUserId,
				maxId,
				direction == EDirection.REFRESH ? EDirection.REFRESH_STR : EDirection.LOADMORE_STR,
				CstOuer.PAGE.DEFAULT_SIZE,
				new OuerFutureListener(this){

					public void onStart(AgnettyResult result) {
						boolean flag = (Boolean)mPtrView.getTag();
						if(!flag) {
							setLoading(true);
						}
					}

					@Override
					public void onComplete(AgnettyResult result) {
						super.onComplete(result);
						boolean flag = (Boolean)mPtrView.getTag();
						if(!flag) {
							mPtrView.setTag(true);
							setLoading(false);
						} else {
							mPtrView.onRefreshComplete();
						}

						List<User> datas = (List<User>) result.getAttach();

						if(direction == EDirection.REFRESH) {
							if(UtilList.isEmpty(datas)) {
								mPtrView.setMode(PullToRefreshBase.Mode.DISABLED);
								setEmptyText(R.string.common_empty_data);
								mAdapter.clearAll();
							} else {
								int count = UtilList.getCount(datas);
								//设置最新的max id
								if(count != 0) {
									mMaxId = datas.get(count - 1).getSortId();
								}

								if(count < CstOuer.PAGE.DEFAULT_SIZE) {
									mPtrView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
								} else {
									mPtrView.setMode(PullToRefreshBase.Mode.BOTH);
								}

								mAdapter.setList(datas);
							}
						} else {
							int count = UtilList.getCount(datas);
							//设置最新的max id
							if(count != 0) {
								mMaxId = datas.get(count - 1).getSortId();
							}

							//数据已读取完
							if(count < CstOuer.PAGE.DEFAULT_SIZE) {
								mPtrView.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
							}

							mAdapter.addList(datas);
						}
					}

					public void onNetUnavaiable(AgnettyResult result) {
						super.onNetUnavaiable(result);
						boolean flag = (Boolean)mPtrView.getTag();
						if(!flag) {
							setRetry(true);
						} else {
							mPtrView.onRefreshComplete();
						}
					}

					@Override
					public void onException(AgnettyResult result) {
						super.onException(result);
						boolean flag = (Boolean)mPtrView.getTag();
						if(!flag) {
							setRetry(true);
						} else {
							mPtrView.onRefreshComplete();
						}
					}
				});

		attachDestroyFutures(future);
	}
	
}
